package com.zhutengwei.android.zuji.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vipul.hp_hp.timelineview.TimelineView;
import com.zhutengwei.android.zuji.R;
import com.zhutengwei.android.zuji.activity.ShowZujiActivity;
import com.zhutengwei.android.zuji.model.ZujiModel;
import com.zhutengwei.android.zuji.tool.Constant;

import java.util.List;

/**
 * Created by HP-HP on 05-12-2015.
 */
public class TimeLineAdapter extends RecyclerView.Adapter<TimeLineViewHolder> {

    private List<ZujiModel> mFeedList;
    private Context mContext;

    public TimeLineAdapter(List<ZujiModel> feedList, Context context) {
        mFeedList = feedList;
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        return TimelineView.getTimeLineViewType(position,getItemCount());
    }

    @Override
    public TimeLineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        mContext = parent.getContext();

        View view;

            view = View.inflate(parent.getContext(), R.layout.item_timeline, null);

        return new TimeLineViewHolder(view, viewType, mContext, mFeedList);
    }

    @Override
    public void onBindViewHolder(TimeLineViewHolder holder, int position) {

        ZujiModel zujiModel = mFeedList.get(position);
        String str = "<font color='#000'><b>" +zujiModel.getTitle() + "</b></font>"
                + "<br /> <font color='#000'><small>类别:" +zujiModel.getCategory()
                +  "<br />时间:" + zujiModel.getDate() + "</small></font>";
        holder.name.setTextSize(18);
        holder.name.setText(Html.fromHtml(str));
    }

    @Override
    public int getItemCount() {
        return (mFeedList!=null? mFeedList.size():0);
    }

}

class TimeLineViewHolder extends RecyclerView.ViewHolder
        implements View.OnClickListener {
    public TextView name;
    public  TimelineView mTimelineView;
    private List<ZujiModel> mZujiModels;
    private Context mContext;

    public TimeLineViewHolder(View itemView, int viewType,
                              Context context, List<ZujiModel> list) {
        super(itemView);
        mContext = context;
        mZujiModels = list;
        name = (TextView) itemView.findViewById(R.id.tx_name);
        mTimelineView = (TimelineView) itemView.findViewById(R.id.time_marker);
        mTimelineView.initLine(viewType);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(mContext, ShowZujiActivity.class);
        intent.putExtra(Constant.DATE, mZujiModels.get(getAdapterPosition()).getDate());
        mContext.startActivity(intent);
    }
}

